package com.github.skystardust.lotteryex.lottery.item.imp.require;

import com.github.skystardust.lotteryex.LotteryEX;
import com.github.skystardust.lotteryex.lottery.item.RequireItem;
import com.github.skystardust.lotteryex.utils.OptionalUtils;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class RequireItemPermission extends RequireItem<String> {
    private boolean takePermission;

    public RequireItemPermission(String requireObject, boolean takePermission) {
        super(requireObject);
        this.takePermission = takePermission;
    }

    public RequireItemPermission(UUID uuid, String requireObject, boolean takePermission) {
        super(uuid, requireObject);
        this.takePermission = takePermission;
    }

    public RequireItemPermission(Map<String, Object> stringObjectMap) {
        super(stringObjectMap);
        setRequireObject(((String) stringObjectMap.get("requireObject")));
        setTakePermission(((boolean) stringObjectMap.get("takePermission")));
    }

    @Override
    public boolean hasRequireObject(Player player) {
        return new OptionalUtils<>(() -> LotteryEX.getLotteryEX().getVaultHook().getPermission())
                .getOptional()
                .map(permission -> permission.has(player, getRequireObject()))
                .orElse(false);
    }

    @Override
    public void takeItem(Player player) {
        if (isTakePermission()) {
            LotteryEX.getLotteryEX().getVaultHook().getPermission().playerRemove(player, getRequireObject());
            new OptionalUtils<>(() -> LotteryEX.getLotteryEX().getVaultHook().getPermission())
                    .getOptional()
                    .ifPresent(permission -> {
                        permission.playerRemove(player, getRequireObject());
                    });
        }
    }


    public boolean isTakePermission() {
        return takePermission;
    }

    public void setTakePermission(boolean takePermission) {
        this.takePermission = takePermission;
    }
}
